package com.ld.hpfs.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ld.hpfs.dao.UserMapper;
import com.ld.hpfs.pojo.Criteria;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

	@Autowired
	private UserMapper userMapper;

	@Override
	public String selectByBaseUser(Criteria criteria) {
		HashMap<String,Object> param = new HashMap<String,Object>();
		param.put("user_name", criteria.get("account"));
		User user = userMapper.checkUser(param);
		if(user!=null){
			if(!user.getPassword().equals(criteria.get("passwordIn"))){
				return "00";
			} else {
				criteria.put("baseUser", user);
				return "01";
			}
		}else{
			return "00";
		}
	}

	public int countByExample(Criteria example) {
		return 0;
	}

	public User selectByPrimaryKey(String userId) {
		return null;
	}

	public List<User> selectByExample(Criteria example) {
		return null;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String deleteByPrimaryKey(Criteria example) {
		int result = 0;

		return result > 0 ? "01" : "00";
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String updateUserPassword(Criteria example) {
		return "";
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String saveUser(Criteria example) {
		return "01";
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String resetPwdByPrimaryKey(Criteria example) {
		return "00";
	}

	/**
	 * 限制密码输入次数
	 * 
	 * @param baseUsers
	 * @param criteria
	 * @return
	 */
	@SuppressWarnings("unused")
	private String loginTimes(User baseUsers, Criteria criteria) {
		String info = "";
		Short errorCount = baseUsers.getErrorCount();
		Date lastLoginTime = baseUsers.getLastLoginTime();
		if(!compareTo(lastLoginTime)){//已经过了XX分钟，那么把errorCount设置为0，重新计数。
			errorCount=(short)0;
		}
		switch (errorCount) {
		case 0:
			// 第一次输入密码错误
			info = "密码错误!你还有2次机会输入密码!";
			errorCount = (short) (errorCount + 1);
			break;
		case 1:
			// 第二次输入密码错误
			info = "密码错误!你还有1次机会输入密码!<br/>如果输入错误，帐户将被锁定不能登录！";
			errorCount = (short) (errorCount + 1);
			break;
		case 2:
			// 第三次输入密码错误
			info = "密码错误!你的帐户已经被锁定！<br/>请联系管理员！";
			errorCount = (short) (errorCount + 1);
			break;
		default:
			break;
		}
		// 保存新的错误次数和时间
		User updateUser = new User();
		updateUser.setUserId(baseUsers.getUserId());
		updateUser.setErrorCount(errorCount);
		updateUser.setLastLoginTime(new Date());
		updateUser.setLastLoginIp(criteria.getAsString("loginip"));
		//this.baseUsersMapper.updateByPrimaryKeySelective(updateUser);
		return info;
	}

	/**
	 * 日期比较
	 * 
	 * @param date
	 * @return
	 */
	private boolean compareTo(Date date) {
		Calendar c = Calendar.getInstance();
		long now = c.getTimeInMillis();
		c.setTime(date);
		long lastly = c.getTimeInMillis();
		// 60分钟1000*60*60;
		//return (now - lastly) <= millis;
		return true;
	}

	@Override
	public String updatePassword(Criteria example) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String findPassword(Criteria example) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String validateAccount(Criteria example) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String updateByPrimaryKeySelective(User user) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public ArrayList<HashMap<String,Object>> getCompany(HashMap<String,Object> param){
		return userMapper.getCompany(param);
	}
	
	@Override
	public HashMap<String,Object> getCompanyId(HashMap<String,Object> param)
	{
		return userMapper.getCompanyId(param);
	}
	
	@Override
	public String getUserState(HashMap<String,Object> param){
		return userMapper.getUserState(param);
	}
	
	@Override
	public HashMap<String,Object> getUserId(HashMap<String,Object> param){
		return userMapper.getUserId(param);
	}
	
	@Override
	public void insertLoginInfo(HashMap<String,Object> temp){
		HashMap<String,Object> user_id = userMapper.getUserId(temp);		
		    if(user_id !=null){
			   temp.put("user_id", user_id.get("USER_HID"));
		    }else{
		   	   temp.put("user_id", -1);
		    }
			HashMap<String,Object> company_id = userMapper.getCompanyIdFromCompany(temp);			
			temp.put("company_id", company_id.get("COMPANY_HID"));
			userMapper.insertLoginInfo(temp);		
		
	}
	
}
